package com.fesine.crm.controller;

import com.fesine.commons.entity.Result;
import com.fesine.commons.util.ResultUtils;
import com.fesine.crm.po.AttendanceInfoPo;
import com.fesine.crm.po.NameComparePo;
import com.fesine.crm.service.AttendanceInfoService;
import com.fesine.crm.service.CrmFileService;
import com.fesine.crm.service.NameCompareService;
import com.fesine.crm.util.FileUtils;
import com.fesine.crm.util.SystemUtils;
import com.fesine.dao.model.QueryResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @description: 功能全部实现
 * @author: Fesine
 * @createTime:2017/10/11 20:10
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/10/11 20:10
 */
@RestController
@RequestMapping("/v1")
public class AttendanceInfoController {
    @Autowired
    AttendanceInfoService service;

    @Autowired
    @Qualifier("attendanceInfoService")
    CrmFileService crmFileService;

    @Autowired
    NameCompareService compareService;

    @Value("${temp.filePath}")
    private String tempFilePath;

    @Value("${temp.deleteFlag}")
    private boolean deleteFlag;

    /**
     * 查询考勤信息
     * @param request
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/attendances")
    public Result list(HttpServletRequest request
            , @RequestParam(required = false) String dateStr
            ,@RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit){
        Integer grade = (Integer) request.getSession().getAttribute("grade");
        AttendanceInfoPo po = new AttendanceInfoPo();
        //普通用户
        if (grade < 1) {
            po.setName((String) request.getSession().getAttribute("name"));
        }
        SystemUtils.setQueryMonth(po, dateStr);
        QueryResult<AttendanceInfoPo> list = service.listPage(po,page,limit);
        return ResultUtils.success(list);
    }

    /**
     * 查询考勤信息
     * @param po
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/attendances/query")
    public Result queryList(AttendanceInfoPo po
            , @RequestParam(required = false) String dateStr
            , @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit){
        SystemUtils.setQueryMonth(po, dateStr);
        if (StringUtils.isBlank(po.getName())) {
            po.setName(null);
        }
        QueryResult<AttendanceInfoPo> list = service.listPage(po,page,limit);
        return ResultUtils.success(list);
    }
    /**
     * 查询个人考勤信息
     * @param po
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/attendance")
    public Result get(AttendanceInfoPo po
            ,@RequestParam(required = false) String dateStr
            , @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit){
        if (StringUtils.isNotBlank(po.getName())) {
            NameComparePo ncp = new NameComparePo();
            ncp.setAttName(po.getName());
            ncp = compareService.get(ncp);
            //根据考勤姓名获取真实姓名
            if (ncp != null) {
                po.setName(ncp.getEmpName());
            }
        }
        SystemUtils.setQueryMonth(po, dateStr);
        QueryResult<AttendanceInfoPo> list = service.listPage(po,page,limit);
        return ResultUtils.success(list);
    }


    /**
     * 导入考勤文件
     * @param file
     * @return
     */
    @PostMapping("/attendance/file")
    public Result uploadFile(@RequestParam("file") MultipartFile file){
        return FileUtils.importFile(file, tempFilePath, deleteFlag, crmFileService);
    }

}
